<?php
	class RegisterView
	{
		// Constants
		const FORMUSERNAME = "username";
		const FORMPASSWORD = "password";
		const FORMCONFIRMPASSWORD = "confirmPassword";
		const FORMGIVENNAME = "givenname";
		const FORMSURNAME = "surname";
		const FORMEMAIL = "email";
		const USERTYPE = "usertype";
		const REGISTERED = "registerAdvanced";
		
		// Returns the register-form formated depending on usertype.
		public function DoRegisterForm($userType)
		{
			$navView = new NavigationView();
			$form = "<form method='post' action=" . $navView->GetFormActionLink() . ">" .
				"<fieldset style='width: 210px'>" .
					"Username: <input id='username' name='" . RegisterView::FORMUSERNAME . "' type='text' /><br />" .	
					"Password: <input id='password' name='" . RegisterView::FORMPASSWORD . "' type='password' /><br />";
					
			if ($userType == null || $userType == UserView::USER)
			{
				
					$form .= "Confirm Password: <input id='confirmpassword' name='" . RegisterView::FORMCONFIRMPASSWORD . "' type='password' /><br />";
			}
			
					$form .= "Given name: <input id='givenname' name='" . RegisterView::FORMGIVENNAME . "' type='text' /><br />" .
					"Surname: <input id='surname' name='" . RegisterView::FORMSURNAME . "' type='text' /><br />" .
					"Email: <input id='email' name='" . RegisterView::FORMEMAIL . "' type='text' /><br />";
			
			if ($userType == UserView::ADMIN)
			{
					$form .= "User type: " . "<select id='usertype' name='" . RegisterView::USERTYPE . "'>" .
						"<option value='0'>User</option>" .
						"<option value='5'>Admin</option>" .
					"</select>";
			}
			
					$form .= "<input id='register' name='" . NavigationView::IWANTTOREGISTER . "' type='submit' value='Register' />" .
				"</fieldset>" .
			"</form>";
				
			return $form;
		}
		
		// Returns the info the user sent via the form as an array.
		public function GetUserInfo($userType)
		{
			// Create an array to store userinfo.
			$userInfo = Array();
			
			// Set the userinfo.
			$userInfo[RegisterView::FORMUSERNAME] = $_POST[RegisterView::FORMUSERNAME];
			$userInfo[RegisterView::FORMPASSWORD] = $_POST[RegisterView::FORMPASSWORD];
			$userInfo[RegisterView::FORMGIVENNAME] = $_POST[RegisterView::FORMGIVENNAME];
			$userInfo[RegisterView::FORMSURNAME] = $_POST[RegisterView::FORMSURNAME];
			$userInfo[RegisterView::FORMEMAIL] = $_POST[RegisterView::FORMEMAIL];
			
			// If the current userType is user or null then set the confirm password.
			if ($userType == UserView::USER)
			{
				$userInfo[RegisterView::FORMCONFIRMPASSWORD] = $_POST[RegisterView::FORMCONFIRMPASSWORD];
			}
			
			// If the current userType is admin then set the usertype for the
			// user you're trying to register aswell.
			if ($userType == UserView::ADMIN)
			{
				$userInfo[RegisterView::USERTYPE] = $_POST[RegisterView::USERTYPE];
			}
			
			return $userInfo;
		}
		
		// Returns wether the user tried to register.
		public function TriedToRegister()
		{
			if(isset($_POST[NavigationView::IWANTTOREGISTER]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns the formated error in case one occurs.
		public function UserErrorMessage($error)
		{
			switch($error)
			{
				case RegisterHandler::INPUTCONTAINSHTML:
					Log::LogError("An error occured.", $error);
					break;
				case RegisterHandler::INPUTWASEMPTY:
					Log::LogMessage($error);
					break;
				case RegisterHandler::INVALIDEMAIL:
					Log::LogMessage($error);
					break;
				case RegisterHandler::INVALIDPASSWORD:
					Log::LogMessage($error);
					break;
				case RegisterHandler::PASSWORDDIDNTMATCH:
					Log::LogMessage($error);
					break;
				case RegisterHandler::USERNAMEXSISTS:
					Log::LogMessage("<font color='red'>" . $error . "</font>");
					break;
			}	
		}
	}		